﻿using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional

namespace TrackSystem.EntityFramework.Migrations
{
    /// <inheritdoc />
    public partial class cm1 : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterDatabase()
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "Channel",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    Name = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false)
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Description = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true)
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    PortCategory = table.Column<int>(type: "int", nullable: false),
                    Ip = table.Column<string>(type: "longtext", nullable: true)
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Port = table.Column<int>(type: "int", nullable: false),
                    localIp = table.Column<string>(type: "longtext", nullable: true)
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    localPort = table.Column<int>(type: "int", nullable: false),
                    COMn = table.Column<string>(type: "longtext", nullable: true)
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Baudrate = table.Column<int>(type: "int", nullable: false),
                    SerialParams = table.Column<string>(type: "longtext", nullable: true)
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Protocol = table.Column<int>(type: "int", nullable: false),
                    optSupport21Cmd = table.Column<bool>(type: "tinyint(1)", nullable: false),
                    optSplit3_4Cmd = table.Column<bool>(type: "tinyint(1)", nullable: false),
                    Interval = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Channel", x => x.Id);
                })
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "Equip",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    Name = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false)
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Description = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false)
                        .Annotation("MySql:CharSet", "utf8mb4")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Equip", x => x.Id);
                })
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "Sensor",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    Name = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false)
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Description = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true)
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Station = table.Column<int>(type: "int", nullable: false),
                    RO_Address = table.Column<int>(type: "int", nullable: false),
                    RO_Length = table.Column<int>(type: "int", nullable: false),
                    RW_Address = table.Column<int>(type: "int", nullable: false),
                    RW_Length = table.Column<int>(type: "int", nullable: false),
                    Eabled = table.Column<bool>(type: "tinyint(1)", nullable: false),
                    channelID = table.Column<int>(type: "int", nullable: false),
                    equipId = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Sensor", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Sensor_Channel_channelID",
                        column: x => x.channelID,
                        principalTable: "Channel",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_Sensor_Equip_equipId",
                        column: x => x.equipId,
                        principalTable: "Equip",
                        principalColumn: "Id");
                })
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "var",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    Name = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false)
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Serial = table.Column<int>(type: "int", nullable: false),
                    snrloopId = table.Column<int>(type: "int", nullable: false),
                    snrloopIndex = table.Column<int>(type: "int", nullable: false),
                    ReadOnly = table.Column<bool>(type: "tinyint(1)", nullable: false),
                    DataType = table.Column<int>(type: "int", nullable: false),
                    InitValue = table.Column<double>(type: "double", nullable: false),
                    ClacuateFormula = table.Column<string>(type: "longtext", nullable: true)
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Description = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true)
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    IsRecord = table.Column<bool>(type: "tinyint(1)", nullable: false),
                    RecordCycle = table.Column<int>(type: "int", nullable: false),
                    AlarmSetting = table.Column<int>(type: "int", nullable: false),
                    HighAlarmSet = table.Column<int>(type: "int", nullable: false),
                    LowAlarmSet = table.Column<int>(type: "int", nullable: false),
                    SensorTableId = table.Column<int>(type: "int", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_var", x => x.Id);
                    table.ForeignKey(
                        name: "FK_var_Sensor_SensorTableId",
                        column: x => x.SensorTableId,
                        principalTable: "Sensor",
                        principalColumn: "Id");
                })
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.InsertData(
                table: "Channel",
                columns: new[] { "Id", "Baudrate", "COMn", "Description", "Interval", "Ip", "Name", "Port", "PortCategory", "Protocol", "SerialParams", "localIp", "localPort", "optSplit3_4Cmd", "optSupport21Cmd" },
                values: new object[,]
                {
                    { 1, 0, "", "Port for TCP", 50, "10.3.41.241", "Port1", 4001, 0, 0, null, "", 0, false, true },
                    { 2, 0, "", "Port for UDP", 50, "127.0.0.1", "Port2", 4002, 1, 0, null, "127.0.0.1", 5002, false, true },
                    { 3, 38400, "COM1", "Port for SERIAL", 50, "", "Port3", 0, 2, 10, "N-8-1", "", 0, false, true }
                });

            migrationBuilder.InsertData(
                table: "Equip",
                columns: new[] { "Id", "Description", "Name" },
                values: new object[,]
                {
                    { 1, "4500m级遥控潜水器", "ROV" },
                    { 2, "4500M中继器", "UPPER" }
                });

            migrationBuilder.InsertData(
                table: "var",
                columns: new[] { "Id", "AlarmSetting", "ClacuateFormula", "DataType", "Description", "HighAlarmSet", "InitValue", "IsRecord", "LowAlarmSet", "Name", "ReadOnly", "RecordCycle", "SensorTableId", "Serial", "snrloopId", "snrloopIndex" },
                values: new object[,]
                {
                    { 1, 0, null, 0, "Var from channel", 0, 0.0, false, 0, "Port1_Enabled", false, 0, null, 2, 1, 0 },
                    { 2, 0, null, 0, "Var from channel", 0, 0.0, false, 0, "Port1_Connected", true, 0, null, 2, 1, 1 },
                    { 3, 0, null, 1, "Var from channel", 0, 0.0, false, 0, "Port1_Cycle", true, 0, null, 2, 1, 2 },
                    { 4, 0, null, 0, "Var from channel", 0, 0.0, false, 0, "Port1_Lapse", true, 0, null, 2, 1, 3 },
                    { 5, 0, null, 0, "Var from channel", 0, 0.0, false, 0, "Port2_Enabled", false, 0, null, 2, 2, 0 },
                    { 6, 0, null, 0, "Var from channel", 0, 0.0, false, 0, "Port2_Connected", true, 0, null, 2, 2, 1 },
                    { 7, 0, null, 1, "Var from channel", 0, 0.0, false, 0, "Port2_Cycle", true, 0, null, 2, 2, 2 },
                    { 8, 0, null, 1, "Var from channel", 0, 0.0, false, 0, "Port2_Lapse", true, 0, null, 2, 2, 3 },
                    { 9, 0, null, 0, "Var from channel", 0, 0.0, false, 0, "Port3_Enabled", false, 0, null, 2, 3, 0 },
                    { 10, 0, null, 0, "Var from channel", 0, 0.0, false, 0, "Port3_Connected", true, 0, null, 2, 3, 1 },
                    { 11, 0, null, 1, "Var from channel", 0, 0.0, false, 0, "Port3_Cycle", true, 0, null, 2, 3, 2 },
                    { 12, 0, null, 1, "Var from channel", 0, 0.0, false, 0, "Port3_Lapse", true, 0, null, 2, 3, 3 }
                });

            migrationBuilder.InsertData(
                table: "Sensor",
                columns: new[] { "Id", "Description", "Eabled", "Name", "RO_Address", "RO_Length", "RW_Address", "RW_Length", "Station", "channelID", "equipId" },
                values: new object[,]
                {
                    { 1, "Power Control", true, "PowerCtl", 0, 0, 0, 2, 1, 1, 1 },
                    { 2, "Leak Detect", true, "LeakDetect", 0, 0, 0, 3, 2, 1, 1 },
                    { 3, "Motor Detect", true, "MotorTemp", 0, 0, 0, 2, 1, 2, 2 },
                    { 4, "CTD Sensor", true, "CTD1", 0, 5, 0, 0, 2, 3, 2 }
                });

            migrationBuilder.CreateIndex(
                name: "IX_Channel_Id",
                table: "Channel",
                column: "Id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Channel_Name",
                table: "Channel",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Equip_Id",
                table: "Equip",
                column: "Id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Equip_Name",
                table: "Equip",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Sensor_channelID",
                table: "Sensor",
                column: "channelID");

            migrationBuilder.CreateIndex(
                name: "IX_Sensor_equipId",
                table: "Sensor",
                column: "equipId");

            migrationBuilder.CreateIndex(
                name: "IX_Sensor_Id",
                table: "Sensor",
                column: "Id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Sensor_Name",
                table: "Sensor",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_var_Id",
                table: "var",
                column: "Id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_var_Name",
                table: "var",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_var_SensorTableId",
                table: "var",
                column: "SensorTableId");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "var");

            migrationBuilder.DropTable(
                name: "Sensor");

            migrationBuilder.DropTable(
                name: "Channel");

            migrationBuilder.DropTable(
                name: "Equip");
        }
    }
}
